package com.adonis.ejoy.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.adonis.ejoy.R;
import com.adonis.ejoy.utils.ChineseUrlUtil;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.squareup.picasso.Picasso;

public class PictureLoopAdapter extends LoopPagerAdapter {
    private String[] pictures;
    private String imgPath;
    private String baseURL;

    public PictureLoopAdapter(RollPagerView viewPager) {
        super(viewPager);
    }

    public void setPictures(String[] pictures) {
        if(pictures.length > 0){
            this.pictures = pictures;
        }else {
            this.pictures = new String[]{"1_2018124202057_p3.jpg"};
        }
    }

    @Override
    public View getView(ViewGroup container, int position) {
        baseURL = container.getContext().getString(R.string.baseURL);
        ImageView view = new ImageView(container.getContext());
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        imgPath = baseURL + "img/" + pictures[position].trim();
        Picasso.with(container.getContext())                     //指定图片的父布局
                .load(ChineseUrlUtil.toBrowserCode(imgPath)) //将图片路径中的中文和空格转换为浏览器可以识别的Base64编码
                .into(view);
        return view;
    }

    @Override
    public int getRealCount() {
        return pictures.length;
    }
}
